package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import dao.ListUserDao;

import util.DbUtil;

public class AddListUserServlet extends HttpServlet {

	DbUtil dbUtil=new DbUtil();

	public void wrong1(){
		String msg="不能为空";
		int type=JOptionPane.YES_NO_CANCEL_OPTION;
		String title="不能为空";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}
	public void wrong2(){
		String msg="密码不一致";
		int type=JOptionPane.YES_NO_CANCEL_OPTION;
		String title="密码不一致";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}
	public void wrong3(){
		String msg="mmm";
		int type=JOptionPane.YES_NO_CANCEL_OPTION;
		String title="mmm";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}
	public void right(){
		String msg="成功";
		int type=JOptionPane.YES_NO_CANCEL_OPTION;
		String title="成功";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String loginName=new String(request.getParameter("loginName").getBytes("ISO-8859-1"),"UTF-8");
		String loginPassword=new String(request.getParameter("loginPassword").getBytes("ISO-8859-1"),"UTF-8");
		String loginPassword2=new String(request.getParameter("loginPassword2").getBytes("ISO-8859-1"),"UTF-8");
		String userName=new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
		String sex=new String(request.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
		String paymentStyle=new String(request.getParameter("paymentStyle").getBytes("ISO-8859-1"),"UTF-8");
		String userStatus=new String(request.getParameter("userStatus").getBytes("ISO-8859-1"),"UTF-8");
		String kaihudate=request.getParameter("year")+"-"+request.getParameter("mouth")+"-"+request.getParameter("day");
		String userEmail=new String(request.getParameter("userEmail").getBytes("ISO-8859-1"),"UTF-8");

		String nationality=new String(request.getParameter("nationality").getBytes("ISO-8859-1"),"UTF-8");
		String career=new String(request.getParameter("career").getBytes("ISO-8859-1"),"UTF-8");
		String userPhone=new String(request.getParameter("userPhone").getBytes("ISO-8859-1"),"UTF-8");
		String company=new String(request.getParameter("company").getBytes("ISO-8859-1"),"UTF-8");
		String mailAddress=new String(request.getParameter("mailAddress").getBytes("ISO-8859-1"),"UTF-8");
		String postCode=new String(request.getParameter("postCode").getBytes("ISO-8859-1"),"UTF-8");

		if(loginName.length()==0||loginPassword.length()==0||loginPassword2.length()==0||userName.length()==0||userPhone.length()==0||userEmail.length()==0){
			wrong1();
			response.sendRedirect("open.jsp");
		}else if(!(loginPassword.equals(loginPassword2))){
			wrong2();
			response.sendRedirect("open.jsp");
		}else{
			try{
				Connection con=null;
				con=dbUtil.getCon();
				ListUserDao listuserdao=new ListUserDao();
				listuserdao.add(con,loginName,loginPassword,userName,sex,paymentStyle,career,company,nationality,userPhone,userEmail,mailAddress,postCode,kaihudate,userStatus);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		response.sendRedirect("ListUserServlet");
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);

	}

}
